<template>
    <el-dialog
        title="账号管理"
        :visible.sync="dialogVisible"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="500px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="登录名" prop="username">
                <el-input v-model="formData.username"/>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
                <el-input v-model="formData.mobile"/>
            </el-form-item>


            <el-form-item label="昵称" prop="nickname">
                <el-input v-model="formData.nickname"/>
            </el-form-item>

            <el-form-item label="真实姓名" prop="realName">
                <el-input v-model="formData.realName"/>
            </el-form-item>

            <el-form-item label="身份证号" prop="idCard">
                <el-input v-model="formData.idCard"/>
            </el-form-item>

            <el-form-item label="选择地区" prop="cityCode">
                <el-cascader
                    v-model="formData.cityCode"
                    :options="options"
                    :props="{ checkStrictly: false, emitPath:false }"
                    clearable
                    style="width:100%;"
                />
            </el-form-item>

            <el-form-item label="详细地址" prop="address">
                <el-input v-model="formData.address"/>
            </el-form-item>

            <el-form-item label="毕业院校" prop="school">
                <el-input v-model="formData.school"/>
            </el-form-item>
            <el-form-item label="管理员ID" prop="adminId">
                <el-input v-model="formData.adminId"/>
            </el-form-item>
            <el-form-item>
                <el-popconfirm :title="'确定保存吗？'" @onConfirm="submitForm()">
                    <el-button slot="reference" type="primary">保存</el-button>
                </el-popconfirm>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import {UserApi} from '@/api/user/User'
import {MessageBox} from 'element-ui'
import {RegionApi} from '@/api/system/RegionApi'

export default {
    name: 'UserAccountAdd',
    components: {},
    data() {
        return {
            formData: {
                uid: 0,
                username: '',
                mobile: '',
                nickname:'',
                realName: '',
                idCard: '',
                cityCode: '',
                address: '',
                school: '',
                adminId:''
            },
            rules: {
                username: [
                    {required: true, message: '请输入用户名', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ],
                mobile: [
                    {required: true, message: '请输入手机号', trigger: 'blur'},
                    {min: 11, max: 11, message: '长度为 11 个字符', trigger: 'blur'}
                ]
            },
            dialogVisible: false,
            options: []
        }
    },
    created() {
        this.getRegion()
    },
    methods: {
        getRegion(){
            RegionApi.getCacheRegion("region_city").then((response)=>{
                this.options = response
            })
        },
        // 显示组件
        show() {
            this.dialogVisible = true
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    that.formData.cityCode = that.formData.cityCode === 0 ? "" : that.formData.cityCode
                    UserApi.insert(that.formData).then((response) => {
                        MessageBox.alert('密码只显示一次，无法找回，请保存。<br/>密码：<b style="color:#ff0000;">' + response + '</b>', '登录密码', {
                            confirmButtonText: '知道了',
                            dangerouslyUseHTMLString: true,
                            callback: () => {
                                that.dialogVisible = false
                                that.$emit('refresh')
                            }
                        })
                    })
                }
            })
        }
    }
}
</script>
